package cn.ainit.sys.socket;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.menu.SessionCont;

@Controller
public class MsgControllor{
	
	private MsgService msgService;
	
	@Resource
	public void setMsgServer(MsgService msgService) {
		this.msgService = msgService;
	}
	
	public static Map<String, List<Map<String, String>>> msgMap = new HashMap<String, List<Map<String, String>>>();
	public static Map<String, List> tsMap = new HashMap<String, List>();
	public static List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
	
	
	
	
	/**
	 * 线程调用加载数据
	 * @author Xuzq
	 * @date 2014-9-12 下午06:26:03
	 */
	public void loadMsg(){
		try {
			list=msgService.loadMsgList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/msg")
	@ResponseBody
	public ReturnObject sendTest(HttpServletRequest request, HttpSession session){
		ReturnObject reobj = new ReturnObject();
		reobj.setMsg("0");
		List<String> msgids = new ArrayList<String>();
		try {
//			msgIdTread.start();
//			Thread.currentThread().sleep(5000);
//			reobj=msgIdTread.getReturnInfo();
			/*long star = System.currentTimeMillis();
			long end = 0;*/
			//设置信息
			StringBuffer msg = new StringBuffer();
			/*while ((end - star) < 30000) {
				end = System.currentTimeMillis();*/
//				if (msgMap.containsKey(session.getAttribute(SessionCont.uid))) {
//					reobj.setMsg("1");
//					reobj.setInfo(formatMsg(msgMap.get(session.getAttribute(SessionCont.uid))));
//					msgids = formatTSList(msgMap.get(session.getAttribute(SessionCont.uid)));
//					//移除数据，执行更新操作
//					msgMap.remove(session.getAttribute(SessionCont.uid));
//					//执行更新操作
//					msgService.upTS(msgids);
//					break;
//				}
				if(list.size()>0){
						for (Map<String, Object> ls:list) {
							if(ls.get("O_ID").equals(session.getAttribute(SessionCont.uid))){
								if(ls.get("DS_ID")!=null&&ls.get("DS_ID")!=""&&ls.get("DJ_ID")!=null&&ls.get("DJ_ID")!="")
								{ 
								String msgs =ls.get("M_MSG").toString();
								if(msgs.indexOf("请审批")!=-1){
									msgs=msgs.substring(0, msgs.indexOf("请审批"))+"请&nbsp;<a href=\"javaScript:void(0)\" style='font-weight:bold ' onclick=\"tranche_msg('"+ls.get("DS_ID")+"','"+ls.get("DJ_ID")+"')\"><u>审批</u></a>"
									     +msgs.substring(msgs.indexOf("审批")+2,msgs.length());
								}else if (msgs.indexOf("请终审")!=-1) {
									msgs=msgs.substring(0, msgs.indexOf("请终审"))+"请&nbsp;<a href=\"javaScript:void(0)\" style='font-weight:bold '  onclick=\"tranche_msg('"+ls.get("DS_ID")+"','"+ls.get("DJ_ID")+"')\"><u>终审</u></a>"
								     +msgs.substring(msgs.indexOf("请终审")+2,msgs.length());
								}else if (msgs.indexOf("请审核")!=-1) {
									msgs=msgs.substring(0, msgs.indexOf("请审核"))+"请 &nbsp;<a href=\"javaScript:void(0)\" style='font-weight:bold '  onclick=\"tranche_msg('"+ls.get("DS_ID")+"','"+ls.get("DJ_ID")+"')\"><u>审核</u></a>"
								     +msgs.substring(msgs.indexOf("请审核")+2,msgs.length());
								}else {
									msgs="<a href=\"javaScript:void(0)\" onclick=\"tranche_msg('"+ls.get("DS_ID")+"','"+ls.get("DJ_ID")+"')\"><U>"+msgs+"</U></a>";
								}
								msg.append(msgs);
								}else {
									msg.append(ls.get("M_MSG"));
								}
								msg.append("</br>");
								msgids.add(ls.get("M_ID").toString());
							}
						}
						if(msg.length()>5){
							msg=msg.delete(msg.length()-5,msg.length());
							reobj.setMsg("1");
							reobj.setInfo(msg.toString());
							msgService.upTS(msgids);
							list.clear();
							/* break;*/
						}
			   }
			/*}*/
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//msgIdTread.stopTrod();
		}
		return reobj;
	}
	
	
}
